package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.conf.DouYingScope;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Hupengpeng
 * @time: 2021/7/2 16:08
 */
public class OauthConnect {
    private static final Log log = LogFactory.get();
    private List<DouYingScope> scopes;//应用授权作用域,多个授权作用域以英文逗号（,）分隔
    private Map<DouYingScope, String> optionalScope;  // 应用授权可选作用域,多个授权作用域以英文逗号（,）分隔，每一个授权作用域后需要加上一个是否默认勾选的参数，1为默认勾选，0为默认不勾选
    private String redirectUri;  // 授权成功后的回调地址，必须以http/https开头。域名必须对应申请应用时填写的域名，如不清楚请联系应用申请人。
    private String state; // 用于保持请求和回调的状态
    private String response_type;
    private String client_key;
    private OauthConnect(){}
    public List<DouYingScope> getScopes() {
        if (scopes == null) {
            throw new DouYinException("应用授权作用域不能为空");
        }
        if (scopes.size() == 0) {
            throw new DouYinException("应用授权作用数量不能为0");
        }
        return scopes;
    }

    public OauthConnect setScopes(List<DouYingScope> scopes) {
        this.scopes = scopes;
        return this;
    }

    public Map<DouYingScope, String> getOptionalScope() {
        return optionalScope;
    }

    public OauthConnect setOptionalScope(Map<DouYingScope, String> optionalScope) {
        this.optionalScope = optionalScope;
        return this;
    }

    public String getRedirectUri() {
        return redirectUri;
    }

    public OauthConnect setRedirectUri(String redirectUri) {
        this.redirectUri = redirectUri;
        return this;
    }

    public String getState() {
        if (StrUtil.isEmpty(state)) {
            return "";
        }
        return state;
    }

    public OauthConnect setState(String state) {
        this.state = state;
        return this;
    }

    public String getClient_key() {
        return client_key;
    }

    public OauthConnect setClient_key(String client_key) {
        this.client_key = client_key;
        return this;
    }

    public String getResponse_type() {
        if (StrUtil.isEmpty(response_type)) {
            response_type = "code";
        }
        return response_type;
    }

    public OauthConnect setResponse_type(String response_type) {
        this.response_type = response_type;
        return this;
    }

    protected static OauthConnect New() {
        return new OauthConnect();
    }

    public String execute() {
        return StrUtil.format("{}{}?client_key={}&response_type={}&scope={}&optionalScope={}&redirect_uri={}&state={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_OAUTH_CONNECT,
                getClient_key(),
                getResponse_type(),
                CollectionUtil.join(getScopes(), ","),
                getOptionalScopeStr(),
                getRedirectUri(),
                getState());
    }

    private String getOptionalScopeStr() {
        if (getOptionalScope() != null) {
            StringBuilder sb = new StringBuilder();
            getOptionalScope().forEach((k, v) -> {
                sb.append(StrUtil.format("{},{}", k, v));
            });
            return sb.toString();
        }
        return "";
    }
}
